package ru.papergames.battleserver.network.serverpackets.flashuser;

import javolution.util.FastMap;
import ru.papergames.battleserver.model.battle.AbstractBattle;
import ru.papergames.battleserver.model.battle.BattlePlayer;
import ru.papergames.common.netutils.ModelWriterUtils;

/**
 * @author atroshanin
 */
public class BattleMemberList extends PaperGameServerPacket {

    private final AbstractBattle battle;
    private final FastMap<Long, BattlePlayer> battleMembersMap;

    public BattleMemberList(AbstractBattle battle) {
        this.battle = battle;
        this.battleMembersMap = battle.getBattleMembers();
    }

    @Override
    protected int getPacketId() {
        return BATTLE_MEMBERS_LIST_PACKET_ID;
    }

    @Override
    protected void writePacketData() {
        writeD(battle.getBattleOrderId());
        writeH(battleMembersMap.size());
        for (FastMap.Entry<Long, BattlePlayer> e = battleMembersMap.head(), end = battleMembersMap.tail(); (e = e.getNext()) != end;) {
            BattlePlayer battlePlayer = e.getValue();
            writeC(battlePlayer.getTeamId());
            writeH(battlePlayer.getBallColor().getId());
            ModelWriterUtils.writePlayerToBuffer(_buf, battlePlayer.getGameUser().getPlayer());
        }
    }

}
